#!/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

from Instagram.SMTP import settings
from Instagram.SMTP.Servers.ServersInterface import ServersInterface


class QQ(ServersInterface):
    def send_mail(self,subject, content,receiver_email,receiver_nickname ):
        qq_config: dict = settings.SMTP['QQ']
        ret = True
        try:
            msg = MIMEText(content, 'plain', 'utf-8')
            msg['From'] = formataddr((qq_config['nickname'], qq_config['email']))  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
            msg['To'] = formataddr((receiver_nickname, receiver_email))  # 括号里的对应收件人邮箱昵称、收件人邮箱账号
            msg['Subject'] = subject  # 邮件的主题，也可以说是标题

            server = smtplib.SMTP_SSL(qq_config['host'], 465)  # 发件人邮箱中的SMTP服务器，端口是25
            server.login(qq_config['email'], qq_config['password'])  # 括号中对应的是发件人邮箱账号、邮箱密码
            server.sendmail(qq_config['email'], [receiver_email, ], msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
            server.quit()  # 关闭连接
        except Exception:  # 如果 try 中的语句没有执行，则会执行下面的 ret=False
            ret = False
        return ret



# if(__name__=='__main__'):
#     manager = QQ()
#     print(manager.send_mail('Test','123','1714255949@qq.com','Receiver'))